package ru.ognick.core;

import ru.ognick.R;
import ru.ognick.locale.ILocale;
import ru.ognick.locale.Locale;
import ru.ognick.option.OptionsManager;
import ru.ognick.utils.Conf;
import ru.ognick.utils.FileManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class CoachActivity extends Activity {
	/** Called when the activity is first created. */
	public final Activity self = this;
	public static final String TAG = "CoachActivity";

	private static final int IDM_CLOSE	= 201;
	private static final int IDM_LANG	= 202;
	private final static int IDD_SAVE   = 101;
	private static final int IDD_LANG	= 102;
	
	static final private ILocale L = Locale.get();

	public final ProcessManager processManager 	= new ProcessManager(this);
	public final OptionsManager optionsManager  = new OptionsManager(this);
	public final FileManager    fileManager     = new FileManager   (this);

	@Override
	public void onPause(){
		super.onPause();
		processManager.finalize();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		processManager.saveState(outState);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		processManager.restoreState(savedInstanceState);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// fileManager.save_def("TrainingProgramm2.js");
		if (Conf.getLastTraing() != null)
			processManager.load(Conf.getLastTraing());

		TabHost tabs = (TabHost) findViewById(R.id.tabhost);
		tabs.setup();
	
		
		
		TabHost.TabSpec spec = tabs.newTabSpec("CORE");
		

		spec.setContent(R.id.scrollView1);
		spec.setIndicator(L.G_TAB_COACH());
		tabs.addTab(spec);

		LinearLayout coreList = (LinearLayout) findViewById(R.id.linearLayout1);
		TextView preloader = new TextView(this,null,android.R.attr.textAppearanceLarge);
		preloader.setText(L.G_CAN_SELECT_PROGRAMM());
		coreList.addView(preloader);

		spec = tabs.newTabSpec("OPTION");
		spec.setContent(R.id.linearLayout3);
		spec.setIndicator(L.G_TAB_OPTION());
		tabs.addTab(spec);
		
		Button newButton = (Button) findViewById(R.id.button1);

		newButton.setText(L.G_BUTTON_NEW_PROGRAMM());
		newButton.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
				showDialog(IDD_SAVE);
			}
		});

		optionsManager.build();

		tabs.setCurrentTab(1);

		tabs.setOnTabChangedListener(new OnTabChangeListener() {
		
			public void onTabChanged(String tabId) {
				if (processManager == null)
					return;
				if (tabId.equals("CORE"))
					processManager.load();
				else
					processManager.pause();
			}
		});
	}


	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		switch (id) {
		case IDD_SAVE:
			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.layout.new_training,
					(ViewGroup) findViewById(R.id.toast_layout));

			final EditText text = (EditText) layout
					.findViewById(R.id.editText1);
			text.setText("NewTraining.js");

			builder.setView(layout);
			builder.setMessage(L.G_SAVE_DIALOG());

			builder.setPositiveButton(L.G_BUTTON_SAVE(),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							if (!fileManager.touch(text.getText().toString()))
								Log.e(TAG, "INSERT ERROR DIALOG");
							optionsManager.build();
						}
					});

			builder.setNegativeButton(L.G_BUTTON_ABORT(),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});

			builder.setCancelable(false);
		case IDD_LANG:
		    builder.setTitle("Lang choose");
		    final CharSequence[] arr = L.langList();
		    int index = -1;
		    for (int i = 0; i < arr.length; i++) 
		    	if (arr[i].equals(L.G_Lang(Conf.getLang())))
		    		index = i; 

		    builder.setSingleChoiceItems(arr,	index, new	DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					Conf.setLang(L.G_Lang(arr[which].toString()));
					dialog.cancel();
					self.finish();
				}
			})
			.setCancelable(false);
			break;
		default:
			return null;
		}
		return builder.create();
	}
	
	@Override 
	public boolean onCreateOptionsMenu (Menu menu) {
		menu.add(Menu.NONE, IDM_CLOSE,	Menu.NONE,	L.G_BUTTON_CLOSE());
		menu.add(Menu.NONE, IDM_LANG,	Menu.NONE,	"LANG");
		return ( super.onCreateOptionsMenu (menu) ) ;
	}
	
	@Override 
	public boolean onOptionsItemSelected (MenuItem	item) {
		switch (item.getItemId()) {
		case IDM_CLOSE:
			  this.finish();
			break;
		case IDM_LANG:
			  showDialog (IDD_LANG);
			break;

		default:
			break;
		}
		return (super.onOptionsItemSelected(item));
	}
}